/*
 * Copyright 2014 Davy Maddelein.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.compomics.pepshell.view.dataviewing;

import com.compomics.pepshell.model.protein.proteinimplementations.PepshellProtein;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 *
 * @author Davy Maddelein
 */
class HistogramPanel extends javax.swing.JPanel {

    private HashMap<Integer, HashSet<PepshellProtein>> distribution;

    /**
     * Creates new form HistogramPanel
     */
    public HistogramPanel() {
        initComponents();
    }

    public void setDistribution(HashMap<Integer, HashSet<PepshellProtein>> aDistribution) {
        this.distribution = aDistribution;
    }

    @Override
    public void paint(Graphics grphcs) {
        super.paint(grphcs);
        grphcs.drawString("number of peptides found per peptide length", 20, 20);
        if (distribution != null) {
            final int xoffset = 100;
            java.util.Set<Integer> var = distribution.keySet();
            List<Integer> sortedPeptideLengthList = Arrays.asList(var.toArray(new Integer[var.size()]));
            Collections.sort(sortedPeptideLengthList);
            for (final Integer peptideLength : sortedPeptideLengthList) {
                grphcs.setColor(Color.orange);
                grphcs.fillRect(xoffset + (peptideLength * 20), 100, 10, distribution.get(peptideLength).size() * 10);
                //todo add mouseover
                this.addMouseListener(new MouseAdapter() {
                    int crystallizedpeptidelength = peptideLength;
                    int xcoordinate = xoffset + (peptideLength * 20);
                    int maxycoordinate = 100 + distribution.get(peptideLength).size() * 10;

                    @Override
                    public void mouseMoved(MouseEvent me) {
                        super.mouseMoved(me);
                        if (me.getX() >= xcoordinate && me.getX() <= xcoordinate + 10 && me.getY() > 100 && me.getY() < maxycoordinate) {
                            for (HashSet<PepshellProtein> pepshellProteins : distribution.values()) {
                                System.out.println(pepshellProteins.size());
                            }
                        }
                        //To change body of generated methods, choose Tools | Templates.
                    }
                });
                grphcs.setColor(Color.BLACK);
                grphcs.drawString(String.valueOf(peptideLength), xoffset + (peptideLength * 20), 90);
                grphcs.drawString(String.valueOf(distribution.get(peptideLength).size()), xoffset + (peptideLength * 20), (distribution.get(peptideLength).size() * 10) + 110);
            }
        }

        //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 631, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
